package com.bnz.controller;

import com.bnz.entity.Student;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;
import com.bnz.vo.StuVo;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/del")
public class RemoveOne extends HttpServlet {
    private StudentService studentService;


    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        req.setCharacterEncoding("utf-8");
        deleteById(req , resp);


    }
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        int flag = studentService.deleteById(Integer.valueOf(id));

        if(flag>0){
            StuVo stu = new StuVo();

            Integer pageNo = 1;

            Integer pageSize = 5;

            PageInfo pageInfo = studentService.queryByPage(stu, pageNo, pageSize);

            req.setAttribute("students", pageInfo.getList());
            req.setAttribute("nowPage", pageInfo.getPages());
            //req.setAttribute("name", stu.getName());
            //req.setAttribute("age", stu.getAge());
            req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req, resp);

        }
        req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req,resp);
    }
}
